package com.cqeec.myblog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqeec.myblog.model.Album;
import com.cqeec.myblog.util.DbUtil;

public class AlbumDaoImpl implements AlbumDao {

	@Override
	public List<Album> selectAlbumByBid(String bid) {
		try {
			Connection con=DbUtil.getDbConnection();
			PreparedStatement ps=con.prepareStatement("select * from album where bid=?");
			ps.setString(1, bid);
			ResultSet rs=ps.executeQuery();
			List<Album> list=new ArrayList<>();
			while(rs.next()) {
				Album album=new Album();
				album.setId(rs.getInt("id"));
				album.setTitle(rs.getString("title"));
				album.setDescrib(rs.getString("describ"));
				String temp=rs.getString("images");
				album.setImages(temp!=null?temp.split(","):new String[0]);
				album.setBid(rs.getString("bid"));
				list.add(album);
			}
			return list;
		} catch (Exception e) {
			return null;
		}
		
		
	}

	@Override
	public void deleteById(String aid) {
        try {
			Connection con=DbUtil.getDbConnection();
			PreparedStatement ps=con.prepareStatement("delete from album where id=?");
			ps.setString(1, aid);
			ps.execute();
		} catch (Exception e) {
			// TODO: handle exception
		}		
	}

	@Override
	public void add(String title, String describ, String bid) {
		try {
			Connection con=DbUtil.getDbConnection();
			PreparedStatement ps=con.prepareStatement("insert into album(title,describ,bid)values(?,?,?)");
			ps.setString(1, title);
			ps.setString(2, describ);
			ps.setString(3, bid);
			ps.execute();
		} catch (Exception e) {
			
		}
	}

	@Override
	public void update(String id, String title, String describ) {
		// TODO Auto-generated method stub
		try {
			Connection con=DbUtil.getDbConnection();
			PreparedStatement ps=con.prepareStatement("update album set title=?,describ=? where id=?");
			ps.setString(1, title);
			ps.setString(2, describ);
			ps.setInt(3, Integer.parseInt(id));
			ps.execute();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	@Override
	public Album selectAlbumById(String id) {
		// TODO Auto-generated method stub
				try {
					Connection con=DbUtil.getDbConnection();
					PreparedStatement ps=con.prepareStatement("select * from album where id=?");
					ps.setString(1, id);
					ResultSet rs=ps.executeQuery();
					Album album=new Album();
					while(rs.next()) {
						album.setId(rs.getInt("id"));
						album.setTitle(rs.getString("title"));
						album.setDescrib(rs.getString("describ"));
						String temp=rs.getString("images");
						album.setImages(temp!=null?temp.split(","):new String[0]);
						album.setBid(rs.getString("bid"));
					}
					return album;
				} catch (Exception e) {
					System.out.println(e);
					return null;
				}
	}

	@Override
	public void updateImagesByid(Album album) {
		try {
			Connection con=DbUtil.getDbConnection();
			PreparedStatement ps=con.prepareStatement("update album set images=? where id=?");
			StringBuffer sb=new StringBuffer();
			for(String temp:album.getImages()) {
				sb.append(temp);
                sb.append(",");				
			}
			ps.setString(1,sb.toString());
			ps.setString(2,album.getId().toString());
			ps.execute();
		} catch (Exception e) {
	 }
	}

}
